package com.ucs.ui;

import com.ucs.domain.Item;
import com.ucs.domain.Order;
import com.ucs.domain.OrderDetail;
import com.ucs.util.UCSData;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class NewOrderActivity extends SimpleActivity implements SimpleActivity.ResultCallbackIF{
	
	public static final String PENDING_ORDER="NewOrderActivity.ORDER";
	private OrderDetailListAdaptor aa;
	private TextView total_items;
	private TextView tv_table;
	private TextView tv_guests;
	
	private Order theOrder = null;
	private Action action = Action.NEW_ORDER;

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);		
		Log.d("ucs", "+NewOrderActivity:onCreate");
		setContentView(R.layout.new_order);	
		
		getOrderAndAction();
		initialize();
		Log.d("ucs", "-NewOrderActivity:onCreate");
		
	}

	private void getOrderAndAction() {
		Bundle extras = getIntent().getExtras();
		if (extras != null){
			theOrder = (Order)extras.getSerializable(PENDING_ORDER);
		}
		if (theOrder != null){
			Log.d("ucs", "previous activity returned order");
			if (theOrder.getOrderID() == 0){
				action = Action.UPDATE_PENDING_ORDER;
				setTitle("Update the Pending Order");
			}
			else{
				action = Action.UPDATE_OPEN_ORDER;
				setTitle("Update the Open Order");
			}
		}
		else{
			theOrder = new Order();
		}
		Log.d("ucs", "action="+action);
	}

	private void initialize() {
		total_items = (TextView)findViewById(R.id.total_items);
		total_items.setText(""+theOrder.getOrderDetails().size());

		final SimpleActivity.ResultCallbackIF thisResultCallbackIF = this;
		tv_table = (TextView)findViewById(R.id.tv_Table);

		tableID = theOrder.getTableID();
		tv_table.setText(TableListAdaptor.getTableName(tableID));

//		if (action == Action.NEW_ORDER){
			tv_table.setClickable(true);
			tv_table.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v) {
					launchSubActivity(SelectTableActivity.class, thisResultCallbackIF, null);				
				}						
			});
//		}
		
		numOfGuests = theOrder.getGuestNum();
		tv_guests = (TextView)findViewById(R.id.tv_Guests);
		tv_guests.setText(""+numOfGuests);		

		tv_guests.setClickable(true);
		tv_guests.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				launchSubActivity(SelectGuestsActivity.class, thisResultCallbackIF, null);
			}
		});
		
		Button btn_add_dish = (Button)findViewById(R.id.btn_add_dish);
		btn_add_dish.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				launchSubActivity(SelectCategoryItemListActivity.class, thisResultCallbackIF, null);
			}
		});
		
		Button btn_delete_order = (Button)findViewById(R.id.btn_delete_dish);
		btn_delete_order.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Log.d("ucs", "delete");
				ListView orders_listview = (ListView)findViewById(R.id.orders_list);
				OrderDetail selectedOrderDetail = (OrderDetail)orders_listview.getSelectedItem();
				Log.d("ucs", "orders_listview.getSelectedItemPosition()"+orders_listview.getSelectedItemPosition());
				if (selectedOrderDetail == null){
					return;
				}
				theOrder.getOrderDetails().remove(selectedOrderDetail);
				aa.notifyDataSetChanged();
			}
		});
		
		Button btn_submit_order = (Button)findViewById(R.id.btn_submit_order);
		if (action != Action.NEW_ORDER){
			btn_submit_order.setText("Update");
		}
		btn_submit_order.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				submitOrder();
				finish();
			}
		});

		ListView orders_listview = (ListView)findViewById(R.id.orders_list);
		orders_listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		aa = new OrderDetailListAdaptor(this, android.R.layout.simple_list_item_1, theOrder.getOrderDetails());
		orders_listview.setAdapter(aa);
		
	}
	
	private void submitOrder() {
		composeNewOrder();
		UCSData.getInstance().addPendingOrder(theOrder);
		Bundle bundle = new Bundle();
		bundle.putSerializable(PENDING_ORDER, theOrder);
		Intent intent = new Intent();
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
	}

	private Order composeNewOrder() {
		theOrder.setTableID(tableID);
		theOrder.setGuestNum(Integer.parseInt(tv_guests.getText().toString()));
		theOrder.setUserID(UCSData.getInstance().getLoggedInUser().getUserID());
		return theOrder;
	}

	private int tableID;
	private int	numOfGuests;
	@Override
	public void resultOk(Intent intent) {
		Bundle extras = intent.getExtras();
		
		Item item = (Item)extras.getSerializable(SelectCategoryItemListActivity.ITEM);
		if (item != null){
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setItemID(item.getItemID());
			theOrder.getOrderDetails().add(orderDetail);
			aa.notifyDataSetChanged();
			total_items.setText(""+theOrder.getOrderDetails().size());	
		}
		
		String table = extras.getString(SelectTableActivity.TABLE_NAME);
		if (table != null){
			tv_table.setText(table);
			tableID = extras.getInt(SelectTableActivity.TABLE_ID);
		}
		
		String guests = extras.getString(SelectGuestsActivity.NUMBER_OF_GUESTS);
		if (guests != null){
			tv_guests.setText(guests);
			numOfGuests = Integer.parseInt(guests);
		}
	}
	public static Order getCreatedOrder(Intent intent) {
		if (intent == null){
			return null;
		}
		Bundle extras = intent.getExtras();
		Order modifiedOrder = null;
		if (extras != null){
			modifiedOrder = (Order)extras.getSerializable(NewOrderActivity.PENDING_ORDER);
		}
		return modifiedOrder;
	}

	@Override
	public void resultCancel(Intent intent) {
		Log.d("ucs", "NewOrderActivity:resultCancel");
	}
	
	enum Action{
		NEW_ORDER,
		UPDATE_PENDING_ORDER,
		UPDATE_OPEN_ORDER
	}
}
